package com.eastjet.maintenance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.maintenance.config.Code;
import com.eastjet.maintenance.domain.Allocate;
import com.eastjet.maintenance.domain.AllocationDetails;
import com.eastjet.maintenance.domain.Annex;
import com.eastjet.maintenance.domain.Documentation;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.mapper.AllocateMapper;
import com.eastjet.maintenance.service.AllocateService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杨博文
 * @description 针对表【allocate(设备调拨)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:37
 */
@Service
public class AllocateServiceImpl extends ServiceImpl<AllocateMapper, Allocate> implements AllocateService {
    @Autowired
    private AllocateMapper allocateMapper;// 设备调拨Mapper
    @Autowired
    private AllocationDetailsServiceImpl allocationDetailsServiceImpl;// 设备调拨明细
    @Autowired
    private DocumentationServiceImpl documentationServiceImpl;// 文档管理
    @Autowired
    private CodeDataFeing codeDataFeing;//自动生成服务

    /*
    新增设备调拨
     */
    @SneakyThrows
    @Override
    public Boolean InsertAllocate(Map<String, Object> map) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "allocate");
        }});
        List<Map<String, Object>> tableData = (List<Map<String, Object>>) map.get("tableData");
        //映射设备调拨对象
        Allocate allocate = new Allocate();
        allocate.setCode(code);
        allocate.setState(0);
        allocate.setTopic(map.get("topic").toString());
        allocate.setApplicant(map.get("applicant").toString());
        allocate.setDepartment(map.get("department").toString());
        allocate.setType(map.get("type").toString());
        allocate.setIllustrate(map.get("illustrate").toString());
        allocate.setSun(tableData.size());
        allocate.setRemark(map.get("remark").toString());
        allocate.setCreatedBy(map.get("applicant").toString());
        allocate.setCreationTime(df.parse(df.format(new Date())));
        allocate.setIs_delete(0);
        boolean save = this.save(allocate);
        //新增设备调拨明细
        if (save) {
            List<AllocationDetails> list = new ArrayList<>();//存储设备调拨明细对象
            //映射设备调拨明细对象
            tableData.forEach(item -> {
                item = (Map<String, Object>) item;
                AllocationDetails p = new AllocationDetails();
                p.setCode(code);
                p.setDeviceNumber(((Map<?, ?>) item).get("code").toString());
                p.setName(((Map<?, ?>) item).get("name").toString());
                p.setDeviceType(((Map<?, ?>) item).get("deviceType").toString());
                p.setSpecification(((Map<?, ?>) item).get("specifications").toString());
                p.setBrand(((Map<?, ?>) item).get("brand").toString());
                p.setPull(((Map<?, ?>) item).get("useDepartments").toString());
                p.setOriginallyResponsible(((Map<?, ?>) item).get("theEquipment").toString());
                p.setStorageLocation(((Map<?, ?>) item).get("storageLocation").toString());
                p.setDispatchDepartment(((Map<?, ?>) item).get("department").toString());
                p.setNewResponsible(((Map<?, ?>) item).get("newPerson").toString());
                p.setTuneLocation(((Map<?, ?>) item).get("newLocation").toString());
                p.setRemark(((Map<?, ?>) item).get("note").toString());
                p.setCreatedBy(map.get("applicant").toString());
                try {
                    p.setCreationTime(df.parse(df.format(new Date())));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                p.setTombstone(0);
                list.add(p);
            });
            Boolean b = allocationDetailsServiceImpl.InsertAllocationDetails(list);
            if (b) {
                List<Map<String, Object>> documents = (List<Map<String, Object>>) map.get("documents");
                List<Documentation> documentations = new ArrayList<Documentation>();// 文档对象集合
                // 循环映射文档对象
                documents.forEach(item -> {
                    item = (Map<String, Object>) item;
                    Documentation documentation = new Documentation();
                    documentation.setCode(code);
                    documentation.setFilename(((Map<?, ?>) item).get("name").toString());
                    try {
                        documentation.setUploadTime(df.parse(((Map<?, ?>) item).get("uploadDate").toString()));
                        documentation.setCreationTime(df.parse(((Map<?, ?>) item).get("uploadDate").toString()));
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    documentation.setUploader(((Map<?, ?>) item).get("uploader").toString());
                    documentation.setSize(((Map<?, ?>) item).get("size").toString());
                    documentation.setCreatedBy(((Map<?, ?>) item).get("uploader").toString());
                    documentations.add(documentation);
                });
                return documentationServiceImpl.InsertList(documentations);
            }
        }
        return false;
    }

    /*
    查询设备调拨
     */
    @Override
    public List<Allocate> SelectAllocate(Allocate allocate) {
        return allocateMapper.SelectAllocate(allocate);
    }

    /*
    更新设备调拨
     */
    @Override
    public Boolean UpdateAllocate(List<Allocate> allocate) {
        return this.updateBatchById(allocate);
    }

    /*
    删除设备调拨
     */
    @Override
    public Boolean DeleteAllocate(List<Allocate> allocate) {
        return this.removeByIds(allocate);
    }
}




